<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class ColumnController extends CommonController {
    //列表
    public function index(){
      $column_object = D('info_column');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($column_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $column_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','content');
      $this->assign('umark','article_column');
      A('list')->setMetaTitle('栏目列表')
          ->setBreadCrumb(array('内容','资讯管理','栏目列表'))
          ->addTopButton('add',array('title'=>'栏目添加'))
          ->addTopButton('resume')
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('title','名称','text')
          ->addTableColumn('upid','上级栏目','getcolumnup')
          ->addTableColumn('submit','投稿','status',60)
          ->addTableColumn('show','显示','status',60)
          ->addTableColumn('sort','排序','sort',80)
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',200,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('forbid')
          ->addRightButton('delete')
          ->fetch();
    }
    //添加
    public function add(){
      $column_object = D('info_column');
      if (IS_POST) {
          $data = $column_object->create();
          if ($data) {
              $id = $column_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'栏目新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'栏目新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$column_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','content');
        $this->assign('umark','article_column');
        $updata = array(array('value'=>0,'title'=>'顶级栏目'));
        $coldata = M('info_column')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
        foreach ($coldata as $key => $value) {
            $up['value'] = $value['id'];
            $up['title'] = $value['title'];
            array_push($updata,$up);
        }
        A('form')->setMetaTitle('栏目添加')
        ->setBreadCrumb(array('内容','资讯管理','栏目列表','栏目添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('title','text','栏目名称','',array(),array('required'=>true))
        ->addFormItem('upid','select','上级栏目','',$updata)
        ->addFormItem('submit','switch','是否允许投稿','禁止后，用户不得投稿，后台可发布',array('允许','禁止'))
        ->addFormItem('show','switch','是否显示','隐藏后，前台此栏目及栏目下文章不可见',array('显示','隐藏'))
        ->addFormItem('sort','text','排序','排序')
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData(array('submit'=>1,'show'=>1,'status'=>1,'sort'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $column_object = D('info_column');
      if (IS_POST) {
          $data = $column_object->create();
          if ($data) {
              $rs = $column_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'栏目编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'栏目编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$column_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','content');
        $this->assign('umark','article_column');
        $id = I('get.ids',0,'intval');
        $form_data = $column_object->find($id);
        $updata = array(array('value'=>0,'title'=>'顶级栏目'));
        $coldata = M('info_column')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
        foreach ($coldata as $key => $value) {
            $up['value'] = $value['id'];
            $up['title'] = $value['title'];
            array_push($updata,$up);
        }
        A('form')->setMetaTitle('栏目编辑')
        ->setBreadCrumb(array('内容','资讯管理','栏目列表','栏目编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('title','text','栏目名称','',array(),array('required'=>true))
        ->addFormItem('upid','select','上级栏目','',$updata)
        ->addFormItem('submit','switch','是否允许投稿','禁止后，用户不得投稿，后台可发布',array('允许','禁止'))
        ->addFormItem('show','switch','是否显示','隐藏后，前台此栏目及栏目下文章不可见',array('显示','隐藏'))
        ->addFormItem('sort','text','排序','排序')
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $column_object = M('info_column');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $column_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'栏目状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'栏目状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $column_object = M('info_column');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $column_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'栏目删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'栏目删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
